package com.article.service.impl;
import javax.annotation.Resource;


import com.article.common.Md5Utils;
import com.article.common.PageParam;
import org.springframework.stereotype.Service;

import com.article.dao.IUserDao;
import com.article.model.User;
import com.article.service.IUserService;

import java.util.List;


@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private IUserDao userDao;

    /**
     * 列表查询
     * @param pageParam
     * @author lx
     * @return
     */
    @Override
    public PageParam queryAll (PageParam pageParam){
        pageParam.setData(this.userDao.queryAll(pageParam));
        pageParam.setCount(pageParam.getData().size());
        pageParam.setCode(0);
        return pageParam;
    }

    /**
     * 根据id获取单条
     * @param id
     * @author lx
     * @return
     */
    @Override
    public User getOne (Integer id){
        return userDao.selectById(id);
    }
    /**
     * 根据account获取单条
     * @param account
     * @author lx
     * @return
     */
    @Override
    public User getOneByAccount (String account){
        return userDao.selectOneByAccount(account);
    }

    /**
     * 新增
     * @param user
     * @author lx
     * @return
     */
    @Override
    public boolean add (User user){
        return userDao.insert(user)>0;
    }

    /**
     * 修改
     * @param user
     * @author lx
     * @return
     */
    @Override
    public boolean update (User user){
        return userDao.updateById(user)>0;
    }

    /**
     * 删除
     * @param id
     * @author lx
     * @return
     */
    @Override
    public boolean delete(Integer id){
        return userDao.deleteById(id)>0;
    }

    /**
     * 批量删除
     * @param ids
     * @author lx
     * @return
     */
    @Override
    public boolean deleteByIds(Integer[] ids){
        for(Integer id:ids){
            userDao.deleteById(id);
        }
        return true;
    }

    @Override
    public boolean restPassword(Integer[] ids){
        for(Integer id:ids){
            User user = this.userDao.selectById(id);
            user.setPassword(Md5Utils.encodeByMd5("123456"));
            this.userDao.updateById(user);
        }
        return true;
    }

}
